<?php
namespace BoxPay\Hooks;
class OrderHooks extends \HooksController {
	/****生成订单之前**/
	function order_before_make(&$param) {
		$order_amount = $param['order_amount'];
		$this->order_amount_dec($param);
	}

	//优惠金额
	function order_amount_dec(&$param) {
		//dump($param);
		$order_amount = $param['order_amount'];
		$card_nums = $param['card_nums'];
		$param['card_nums'] = array();
		/******使用卡券，多个使用逗号分开***/
		if ($card_nums && $order_amount > 0) {
			$param['card_num_money'] = 0;
			foreach ($card_nums as $card_num) {
				$data = Logic("BoxMall", "Card")->check_card($card_num);
				Logic("BoxMall", "Card")->use_card($param['user_id'], $card_num);
				$money = $data['money'];
				if ($money && $order_amount > 0) {
					$order_amount -= $money;
					$param['card_nums'][] = $card_num;
					$param['card_num_money'] += $money;
				} else {
					$order_amount = 0;
					break;
				}
			}
		}
		/****使用余额***/
		$user_info = BoxModel("system_member")->where(array('user_id' => $param['user_id']))->find();
		if ($param['use_yie'] && $order_amount > 0) {
			if ($param['use_yie'] > $user_info['money']) {
				$param['use_yie'] = $user_info['money'];
			}
			if ($param['use_yie'] > $order_amount) {
				$param['use_yie'] = $order_amount;
			}
			$order_amount -= $param['use_yie'];
			/******使用课程学币***/
			$num = $param['use_yie'];
			/***余额冻结*/
			if ($num > 0) {
				$bool = BoxModel("system_member")->where(array('user_id' => $param['user_id']))->setDec("money", $num);
				if ($bool) {
					BoxModel("system_money_freeze")->add(array(
						'user_id' => $param['user_id'],
						'money' => $num,
						'addtime' => date("Y-m-d H:i:s"),
						'order_sn' => $param['order_sn'],
						//'user_id' => $param['user_id'],
					));
				}
				//$r = Logic('BoxSns', 'Log')->add($row);
			}
		}

		$param['order_amount'] = $order_amount;
		hook("order_amount_dec_" . $param['order_type'], $param);
	}

}
